package com.sk.bilibili.other;

import com.sk.bilibili.bean.Member;
import com.sk.bilibili.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author 邵康
 * @date 2021/12/4 10:45
 */
//@Component
public class MemberArgumentResolver implements HandlerMethodArgumentResolver {
    @Autowired//推荐使用容器创建 可以自动填充资源
    private MemberService memberService;
    @Override//判断是否支持该参数类型解析
    public boolean supportsParameter(MethodParameter parameter) {
        return Member.class.equals(parameter.getParameterType());
    }
    @Override//对该参数填充对象进行解析
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                  NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        return memberService.getById(id);
    }
}
